<template>
  <div class="p-[var(--app-content-padding)]">
    <!-- 搜索工作栏 -->
    <ContentWrap class="form-card">
      <el-form class="-mb-15px mt-4px" :model="queryParams" ref="queryFormRef" :inline="true">
        <el-form-item label="邮箱" prop="mail">
          <el-input
            v-model="queryParams.mail"
            placeholder="请输入邮箱"
            clearable
            @keyup.enter="setSearchParams"
            class="!w-240px"
          />
        </el-form-item>

        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="queryParams.username"
            placeholder="请输入用户名"
            clearable
            @keyup.enter="setSearchParams"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item class="float-right" style="margin-right: 0">
          <el-button type="primary" @click="setSearchParams">
            <Icon icon="ep:search" />
            <span class="mt-1px">搜索</span>
          </el-button>
          <el-button @click="resetQuery">
            <Icon icon="ep:refresh" />
            <span class="mt-1px">重置</span>
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap class="mb-0">
      <div class="flex justify-between mb-[12px]">
        <div>
          <el-button
            type="primary"
            @click="openForm('create')"
            v-hasPermi="['system:mail-account:create']"
          >
            <Icon icon="ep:plus" />
            <span class="mt-1px">新增</span>
          </el-button>
        </div>
      </div>

      <Table
        :columns="allSchemas.tableColumns"
        :data="tableObject.tableList"
        :loading="tableObject.loading"
        v-model:pageSize="tableObject.pageSize"
        v-model:currentPage="tableObject.currentPage"
        :height="tableHeight"
      >
        <template #action="{ row }">
          <el-tooltip content="编辑" placement="top">
            <el-button
              link
              type="primary"
              @click="openForm('update', row.id)"
              v-hasPermi="['system:mail-account:update']"
            >
              <Icon icon="iconamoon:edit-light" />
            </el-button>
          </el-tooltip>

          <el-tooltip content="详情" placement="top">
            <el-button
              link
              type="primary"
              @click="openDetail(row.id)"
              v-hasPermi="['system:mail-account:query']"
            >
              <Icon icon="bx:message-detail" />
            </el-button>
          </el-tooltip>

          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="danger"
              v-hasPermi="['system:mail-account:delete']"
              @click="handleDelete(row.id)"
            >
              <Icon icon="material-symbols:delete-outline" />
            </el-button>
          </el-tooltip>
        </template>
      </Table>
      <!-- 分页 -->
      <Pagination
        v-model:limit="tableObject.pageSize"
        v-model:page="tableObject.currentPage"
        :total="tableObject.total"
      />
    </ContentWrap>
  </div>

  <!-- 表单弹窗：添加/修改 -->
  <MailAccountForm ref="formRef" @success="getList" />
  <!-- 详情弹窗 -->
  <MailAccountDetail ref="detailRef" />
</template>
<script lang="ts" setup>
import { allSchemas } from './account.data'
import * as MailAccountApi from '@/api/system/mail/account'
import MailAccountForm from './MailAccountForm.vue'
import MailAccountDetail from './MailAccountDetail.vue'
import { useTableHeight } from '@/hooks/web/useTableHeight'

defineOptions({ name: 'SystemMailAccount' })
const queryFormRef = ref() // 搜索的表单
const queryParams = reactive({
  pageNo: 1,
  pageSize: 20,
  mail: '',
  username: ''
})

const { tableHeight } = useTableHeight({
  extraHeight: 208
})

// tableObject：表格的属性对象，可获得分页大小、条数等属性
// tableMethods：表格的操作对象，可进行获得分页、删除记录等操作
const { tableObject, tableMethods } = useTable({
  getListApi: () => MailAccountApi.getMailAccountPage(queryParams), // 分页接口
  delListApi: MailAccountApi.deleteMailAccount // 删除接口
})
// 获得表格的各种操作
const { getList, setSearchParams } = tableMethods

const resetQuery = () => {
  queryFormRef.value.resetFields()
  getList()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 详情操作 */
const detailRef = ref()
const openDetail = (id: number) => {
  detailRef.value.open(id)
}

/** 删除按钮操作 */
const handleDelete = (id: number) => {
  tableMethods.delList(id, false)
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
